using System;
using System.Globalization;
using System.Text;
using BizTalk.BuildGenerator.MetaData;

namespace BizTalk.BuildGenerator.BuildFrameworks.Default.CommandBuilders
{
    /// <summary>
    /// Creates a command to publish web services from the build process
    /// </summary>
    public class PublishedWebServicesCommandBuilder : ICommandBuilder
    {
        private const string PublishSoapWebServiceCommand =
            "\t\t<BizTalk.BuildGenerator.Tasks.WebServiceGenerator WebServiceDescriptionPath=\"{0}\" />";

        //Removed for WSE 2 obselete
        //private const string PublishWse2WebServiceCommand =
        //    "\t\t<BizTalk.BuildGenerator.Tasks.WseWebServiceGenerator WebServiceDescriptionPath=\"{0}\" />";

        private const string PublishWcfServiceCommand =
            "\t\t<BizTalk.BuildGenerator.Tasks.WcfServiceGenerator DescriptionPath=\"{0}\" />";

        private const string CommandTag = "<!-- @PublishWebServicesCommand@ -->";

        #region ICommandBuilder Members

        /// <summary>
        /// Implements the build method
        /// </summary>
        /// <param name="args"></param>
        /// <param name="fileBuilder"></param>
        public void Build(GenerationArgs args, StringBuilder fileBuilder)
        {
            StringBuilder commandBuilder = new StringBuilder();

            foreach (var ws in args.WebServices.WebServices)
            {
                string command = null;
                switch (ws.Type)
                {
                    case WebServiceTypes.Soap:
                        command = string.Format(CultureInfo.InvariantCulture, PublishSoapWebServiceCommand, ws.WebServiceDescriptionPath);
                        break;
                    case WebServiceTypes.Wcf:
                        command = string.Format(CultureInfo.InvariantCulture, PublishWcfServiceCommand, ws.WebServiceDescriptionPath);
                        break;
                    default:
                        break;
                }

                commandBuilder.Append(command);
                commandBuilder.Append(Environment.NewLine);
            }

            fileBuilder.Replace(CommandTag, commandBuilder.ToString());
        }

        #endregion
    }
}